# -*- coding: utf-8 -*-

class SSHManager:
    def __init__(self):
        self.connections = []
        self.groups = []
        
    def add_group(self, name):
        """添加分组"""
        group = {"id": len(self.groups), "name": name, "connections": []}
        self.groups.append(group)
        return group
        
    def add_connection(self, group_id, host, port, username, password=None, key_file=None):
        """添加连接"""
        connection = {
            "id": len(self.connections),
            "host": host,
            "port": port,
            "username": username,
            "password": password,
            "key_file": key_file
        }
        self.connections.append(connection)
        
        # 添加到分组
        for group in self.groups:
            if group["id"] == group_id:
                group["connections"].append(connection)
                break
                
        return connection
        
    def remove_group(self, group_id):
        """删除分组"""
        self.groups = [g for g in self.groups if g["id"] != group_id]
        
    def remove_connection(self, connection_id):
        """删除连接"""
        self.connections = [c for c in self.connections if c["id"] != connection_id]
        for group in self.groups:
            group["connections"] = [c for c in group["connections"] if c["id"] != connection_id]
            
    def get_groups(self):
        """获取所有分组"""
        return self.groups
        
    def get_connections(self):
        """获取所有连接"""
        return self.connections